package com.water.app.Controller.image;

import com.qiniu.util.Auth;
import com.water.app.Constant;
import com.water.app.QiniuUtil;
import com.water.app.Result;
import com.water.app.interfaces.Goods.shopService;
import com.water.app.interfaces.account.userService;
import com.water.app.pictureToken;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/api/image")
public class imageController {
    private static final Logger log = LoggerFactory.getLogger(imageController.class);
    @Autowired
    private QiniuUtil qiniuUtil;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private userService userService;
    @Autowired
    private shopService shopService;

    /**
     * 商品图片上传接口
     */
    @PostMapping("/uploadShopImage")
    public Result uploadShopImage(@RequestParam("file") MultipartFile file,
                                  @RequestParam("shopId") Integer shopId){
        try {
//            userImage;
//            goodsCommentImage;
//            shopImage;
            // 校验文件（格式、大小）
            if (!file.getContentType().startsWith("image/")) {
                return Result.error("请上传图片文件");
            }
            if (file.getSize() > 20 * 1024 * 1024) { // 限制10MB
                return Result.error("图片大小不能超过20MB");
            }
            if(shopId==null){
                log.info("shopId传参错误:{}",shopId);
                return Result.error("shopId传参错误");
            }
            Integer userId = Constant.THREAD_LOCAL.get();
            // 上传到七牛云，存储到“goods-images/”文件夹
            String picture = qiniuUtil.uploadImage(file, Constant.SHOP_IMAGES,"userId/"+ userId);

            if(picture==null||picture.isEmpty()){
                return Result.error("上传图片失败");
            }else {
                boolean result = shopService.updatePicture(picture, shopId);
                if(result) {
                    log.info("上传图片成功");
                    return Result.success(1, "上传图片成功", picture);
                }else {
                    log.info("上传图片失败");
                    return Result.error("上传图片失败");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("上传失败：" + e.getMessage());
        }
    }

    @PostMapping("/uploadUserImage")
    public Result uploadGoodsImage(@RequestParam("file") MultipartFile file) {
        try {
//            userImage;
//            goodsCommentImage;
//            shopImage;
            // 校验文件（格式、大小）
            if (!file.getContentType().startsWith("image/")) {
                return Result.error("请上传图片文件");
            }
            if (file.getSize() > 20 * 1024 * 1024) { // 限制10MB
                return Result.error("图片大小不能超过20MB");
            }
            Integer userId = Constant.THREAD_LOCAL.get();
            // 上传到七牛云，存储到“goods-images/”文件夹
            String picture = qiniuUtil.uploadImage(file,Constant.SHOP_IMAGES, "userId/"+ userId);
            log.info("picture:{}",picture);
            if(picture==null||picture.isEmpty()){
                return Result.error("上传图片失败");
            }else {
                userService.updateUserPicture(picture,userId);
                return Result.success(1,"上传图片成功",picture);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("上传失败：" + e.getMessage());
        }
    }
}
